package cn.micro.system.manage.common.utils;

import cn.micro.system.manage.entity.SysDimDept;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class DeptHelper {

    //构建树形结构
    public static List<SysDimDept> bulidTree(List<SysDimDept> sysDimDeptList) {
//        创建集合封装最终数据
        List<SysDimDept> trees = new ArrayList<>();
        //遍历所有菜单集合
        for (SysDimDept sysDimDept:sysDimDeptList) {
            //找到递归入口，parentid=0
            if(sysDimDept.getParentId() ==0) {
                trees.add(findChildren(sysDimDept,sysDimDeptList));
            }
        }
        return trees;
    }

    //从根节点进行递归查询，查询子节点
    // 判断 id =  parentid是否相同，如果相同是子节点，进行数据封装

    public static SysDimDept findChildren(SysDimDept sysDimDept,List<SysDimDept> treeNodes){
        sysDimDept.setChildren(new ArrayList<SysDimDept>());
        //遍历递归查找
        for (SysDimDept it:treeNodes) {
            //比对
            if(Objects.equals(Long.parseLong(sysDimDept.getId()), it.getParentId())) {
                if(sysDimDept.getChildren()==null) {
                    sysDimDept.setChildren(new ArrayList<>());
                }
                sysDimDept.getChildren().add(findChildren(it,treeNodes));
            }
        }
        return sysDimDept;
    }

    //构建树形结构
    public static List<SysDimDept> bulidDropTree(List<SysDimDept> sysDimDeptList) {
//        创建集合封装最终数据
        List<SysDimDept> trees = new ArrayList<>();
        //遍历所有菜单集合
        for (SysDimDept sysDimDept:sysDimDeptList) {
            //找到递归入口，parentid=0
            if(sysDimDept.getParentId() ==0) {
                trees.add(findDropChildren(sysDimDept,sysDimDeptList));
            }
        }
        return trees;
    }

    //从根节点进行递归查询，查询子节点
    // 判断 id =  parentid是否相同，如果相同是子节点，进行数据封装

    public static SysDimDept findDropChildren(SysDimDept sysDimDept,List<SysDimDept> treeNodes){
        //遍历递归查找
        for (SysDimDept it:treeNodes) {
            //比对
            if(Objects.equals(Long.parseLong(sysDimDept.getId()), it.getParentId())) {
                if(sysDimDept.getChildren()==null) {
                    sysDimDept.setChildren(new ArrayList<>());
                }
                sysDimDept.getChildren().add(findDropChildren(it,treeNodes));
            }
        }
        return sysDimDept;
    }

}
